package com.cv.framework.web.core.utils;

import cn.hutool.core.util.ObjUtil;
import com.cv.framework.common.enums.UserTypeEnum;
import com.cv.framework.common.pojo.Result;
import com.cv.framework.web.config.WebProperties;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * Web 通用工具类
 *
 * @author Charles_XDXD
 */
public class WebFrameworkUtils {

    private static final String REQUEST_ATTRIBUTE_LOGIN_USER_ID = "login_user_id";

    private static final String REQUEST_ATTRIBUTE_LOGIN_USER_TYPE = "login_user_type";

    private static final String REQUEST_ATTRIBUTE_RESULT = "common_result";

    public static final String USER_AUTH = "user-auth";

    private static WebProperties properties;

    public WebFrameworkUtils(WebProperties webProperties) {
        WebFrameworkUtils.properties = webProperties;
    }

    public static void setLoginUserId(ServletRequest request, Long userId) {
        request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_ID, userId);
    }

    public static Long getLoginUserId(ServletRequest request) {
        return (Long) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_ID);
    }

    public static Long getLoginUserId() {
        HttpServletRequest request = getRequest();
        if (ObjUtil.isEmpty(request)) {
            return null;
        }
        return getLoginUserId(request);
    }

    public static void setLoginUserType(ServletRequest request, Integer userType) {
        request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_TYPE, userType);
    }

    public static Integer getLoginUserType(HttpServletRequest request) {
        if (request == null) {
            return null;
        }

        Integer userType = (Integer) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_TYPE);
        if (userType != null) {
            return userType;
        }

        // 除了系统、审核模块，其他模块默认为普通用户
        if (request.getServletPath().startsWith(properties.getSystemApi().getPrefix()) ||
                request.getServletPath().startsWith(properties.getAuditApi().getPrefix())) {
            return UserTypeEnum.ADMIN.getValue();
        }

        return UserTypeEnum.MEMBER.getValue();
    }

    public static Integer getLoginUserType() {
        HttpServletRequest request = getRequest();
        return getLoginUserType(request);
    }

    public static void setResult(ServletRequest request, Result<?> result) {
        request.setAttribute(REQUEST_ATTRIBUTE_RESULT, result);
    }

    public static Result<?> getResult(ServletRequest request) {
        return (Result<?>) request.getAttribute(REQUEST_ATTRIBUTE_RESULT);
    }

    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        if (!(requestAttributes instanceof ServletRequestAttributes)) {
            return null;
        }

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        return servletRequestAttributes.getRequest();
    }

}
